package tree.binary;

import entity.tree.TreeNode;

public class leetcode606根据二叉树创建字符串 {
    public String tree2str(TreeNode root) {
        String str = dfs(root);
        return str.substring(1,str.length() - 1);
    }

    public String dfs(TreeNode root) {
        String result = "";
        if (root == null) {
            return result;
        }
        result += "(" + root.val;
        if (root.left == null && root.right != null) {
            result += "()";
        } else {
            result += dfs(root.left);
        }
        result += dfs(root.right);
        result += ")";
        return result;
    }
}
